package structure;


public class MyQueue {
    // 队列
    private int[] array;
    // 队列长度
    private int length;
    // 队列头
    private int head;
    // 队列尾
    private int tail;
    // 队列大小
    private int size;

    // 初始化队列
    public MyQueue(int length) {
        this.length = length;
        array = new int[length];
        head = 0;
        tail = -1;
        size = 0;
    }

    // 入队
    public void enqueue(int value) {
        if (size == length) {
            System.out.println("队列已满");
            return;
        }
        tail = (tail + 1) % length;
        array[tail] = value;
        size++;
    }

    // 出队
    public int dequeue() {
        if (size == 0) {
            System.out.println("队列已空");
            return -1;
        }
        int value = array[head];
        head = (head + 1) % length;
        size--;
        return value;
    }
}
